package laserdancer.smart.com.criminalintent;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import butterknife.ButterKnife;

/**
 * Created by ares on 2018/1/30.
 */

public class CrimeListFragment extends Fragment {

    private final static String TAG="CrimeListFragment";
    private static final int REQUEST_CRIME = 1;
    public final static String CRIMEID = "CrimeId";

    private boolean mShowSubtitle= false;

    private RecyclerView mRecyclerView;
    private CrimeAdapter mCrimeAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }


    @Override
    public View onCreateView(LayoutInflater _inflater, ViewGroup container,Bundle _SavedInstanceState){

        View _view = _inflater.inflate(R.layout.fragment_crime_list,container,false);

        mRecyclerView = _view.findViewById(R.id.crime_recycler_view);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        updateUI();

        return _view;
    }

    @Override
    public void onResume(){
        super.onResume();
        updateUI();
    }

    @Override
    public void onCreateOptionsMenu(Menu _menu, MenuInflater _inflater){
        super.onCreateOptionsMenu(_menu,_inflater);
        _inflater.inflate(R.menu.fragment_crime_list,_menu);
    }

    @Override
    public void onActivityResult(int _request_code,int _result_code,Intent _data){
        if(_request_code == REQUEST_CRIME){

        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()){
            case R.id.menu_item_viwe_crime:
                Crime _crime =new Crime();
                CrimeLab.get(getActivity()).addCrime(_crime);

                Intent i = CrimePageActivity.newIntent(getActivity(),_crime.getId());
                startActivity(i);
                return true;
            case R.id.menu_item_show_subtitle:
                mShowSubtitle = !mShowSubtitle;
                UpdateSubtitle(mShowSubtitle);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void UpdateSubtitle(boolean showSubtitle){
        CrimeLab _crimeLab = CrimeLab.get(getActivity());
        int size = _crimeLab.getCrimes().size();
        String subTitle = "we got " +size;
        if(!mShowSubtitle){
            subTitle = null;
        }
        AppCompatActivity _activity = (AppCompatActivity)getActivity();
        _activity.getSupportActionBar().setSubtitle(subTitle);
    }

    private void updateUI(){
        CrimeLab _crimeLab = CrimeLab.get(getActivity());
        List<Crime> _crimes = _crimeLab.getCrimes();
        if(mCrimeAdapter == null) {
            mCrimeAdapter = new CrimeAdapter(_crimes);
            mRecyclerView.setAdapter(mCrimeAdapter);
        }
        else{
            mCrimeAdapter.notifyDataSetChanged();
        }
    }

    private class CrimeAdapter extends RecyclerView.Adapter<CrimeHolder>{

        private List<Crime> mCrimes;

        public CrimeAdapter(List<Crime> _crimes){
            mCrimes = _crimes;
        }

        @Override
        public CrimeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater _layoutInflater = LayoutInflater.from(getActivity());

            View _view = _layoutInflater.inflate(R.layout.list_item_crime,parent,false);

            return new CrimeHolder(_view);
        }

        @Override
        public void onBindViewHolder(CrimeHolder holder, int position) {

            Crime _crime = mCrimes.get(position);
            holder.bindCrime(_crime);
        }

        @Override
        public int getItemCount() {
            return mCrimes.size();
        }
    }

    private class CrimeHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private Crime mCrime;

        public TextView mTitleTextView;
        public TextView mDateTextView;
        public CheckBox mSolvedCheckBox;

        public CrimeHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
            itemView.setOnClickListener(this);

            mTitleTextView = (TextView) itemView.findViewById(R.id.list_item_crime_title_view);
            mDateTextView = (TextView)itemView.findViewById(R.id.list_item_crime_date_text_view);
            mSolvedCheckBox = (CheckBox) itemView.findViewById(R.id.list_item_crime_solved_checkbox);
        }

        public void bindCrime(Crime _crime){
            mCrime = _crime;
            mTitleTextView.setText(mCrime.getTitle());
            mDateTextView.setText(mCrime.getDate().toString());
            mSolvedCheckBox.setChecked(mCrime.isSolved());
        }

        @Override
        public void onClick(View v) {

            //Intent intent =new Intent(getActivity(),CrimeActivity.class);
            Intent intent = CrimePageActivity.newIntent(getActivity(),mCrime.getId());
            intent.putExtra(CrimeListFragment.CRIMEID,mCrime.getId());
            startActivity(intent);
            //startActivityForResult(intent,REQUEST_CRIME);
        }

    }

}
